package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.ccl.iot.R;
import com.ccl.iot.publics.UIUtils;

/**
 * 创建者     王浩
 * 创建时间   2017/2/24 9:32
 * 描述	      ${TODO}
 */
public class CorrectLocationView extends View {

    private final int DRAW_SPEED = 5;

    private Paint mAnimPaintG, mAnimPaintDG, mBeaconPaint, mUserPaint;
    private RadialGradient mRadialGradient, mRadialGradient2;
    private int mHeight;
    private int mWidth;
    private int mBeaconRadius;

    private int mFullRadius;

    private int mGRaios = 1;
    private int mDGRaios = 0;
    private int mD;
    private int mE;
    private int mXRadius;
    private int mYRadius;
    private Bitmap mUser;

    private int mAngle = 2;

    public static float LOCATION_ACCURACY = 0.8f;

    public CorrectLocationView(Context context) {
        this(context, null);
    }

    public CorrectLocationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CorrectLocationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        mAnimPaintG = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAnimPaintG.setStyle(Paint.Style.FILL);
        mD = Color.parseColor("#dddddd");
        //        mAnimPaintG.setColor(mD);

        mAnimPaintDG = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAnimPaintDG.setStyle(Paint.Style.FILL);
        mE = Color.parseColor("#eeeeee");
        //        mAnimPaintDG.setColor(mE);

        mBeaconPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBeaconPaint.setStyle(Paint.Style.FILL);
        mBeaconPaint.setColor(Color.GREEN);

        mUserPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //        mUserPaint.setStyle(Paint.Style.FILL);
        //        mUserPaint.setColor(Color.RED);

        mBeaconRadius = UIUtils.dp2px(10);

        mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_red_user), mBeaconRadius << 1, mBeaconRadius << 1);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = getHeight() - mBeaconRadius * 2;
        mWidth = getWidth() - mBeaconRadius * 2;

        //        mHeight = getHeight() - mUser.getHeight();
        //        mWidth = getWidth() - mUser.getWidth();

        mXRadius = mWidth / 2;
        mYRadius = mHeight / 2;

        mFullRadius = mXRadius > mYRadius ? mYRadius : mXRadius;

        mUserCirRadius = mYRadius;

        mRadialGradient = new RadialGradient(mXRadius, mYRadius, mFullRadius,
                new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

        mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mFullRadius,
                new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

        mAnimPaintG.setShader(mRadialGradient);
        mAnimPaintDG.setShader(mRadialGradient2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isOnCenter) {
            mUserPaint.setColor(Color.RED);
            canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mFullRadius, mUserPaint);
            mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_green_user), mBeaconRadius << 1, mBeaconRadius << 1);
            canvas.drawBitmap(mUser, mXRadius + mBeaconRadius - mUser.getWidth()/2, mYRadius + mBeaconRadius - mUser.getHeight()/2, mUserPaint);
        } else {

            if (mGRaios > mDGRaios) {

                canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mFullRadius, mAnimPaintDG);
                mGRaios += DRAW_SPEED;

                if (mGRaios > mFullRadius) {
                    mGRaios = mFullRadius;
                }
                mRadialGradient = new RadialGradient(mXRadius, mYRadius, mGRaios,
                        new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

                mAnimPaintG.setShader(mRadialGradient);
                canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mGRaios, mAnimPaintG);

                if (mGRaios >= mFullRadius / 2) {
                    mDGRaios += DRAW_SPEED;

                    if (mDGRaios > mFullRadius) {
                        mDGRaios = mFullRadius;
                    }

                    mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mDGRaios,
                            new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

                    mAnimPaintDG.setShader(mRadialGradient2);
                    canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mDGRaios, mAnimPaintDG);
                }
            } else {
                canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mFullRadius, mAnimPaintG);
                mDGRaios += DRAW_SPEED;

                if (mDGRaios > mFullRadius) {
                    mDGRaios = mFullRadius;
                }

                mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mDGRaios,
                        new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

                mAnimPaintDG.setShader(mRadialGradient2);
                canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mDGRaios, mAnimPaintDG);

                if (mDGRaios >= mFullRadius / 2) {
                    mGRaios += DRAW_SPEED;

                    if (mGRaios > mFullRadius) {
                        mGRaios = mFullRadius;
                    }

                    mRadialGradient = new RadialGradient(mXRadius, mYRadius, mGRaios,
                            new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

                    mAnimPaintG.setShader(mRadialGradient);
                    canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mGRaios, mAnimPaintG);
                }
            }

            canvas.drawCircle(mXRadius + mBeaconRadius, mYRadius + mBeaconRadius, mBeaconRadius, mBeaconPaint);
            int x = (int) (mXRadius + mUserCirRadius * Math.cos(Math.toRadians(mAngle)));
            int y = (int) (mYRadius + mUserCirRadius * Math.sin(Math.toRadians(mAngle)));
            canvas.drawBitmap(mUser, x, y, mUserPaint);

            mAngle = (mAngle % 360) + 2;
            if (mGRaios >= mFullRadius) {
                mGRaios = 0;
            }

            if (mDGRaios >= mFullRadius) {
                mDGRaios = 0;
            }

            postDelayed(drawTask, 20);
        }
    }

    private Runnable drawTask = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

    /**
     * 设置百分比 0~1
     */
    private int mUserCirRadius;
    private boolean isEdge = false;

    public void setPercentage(float percentage) {
        if (percentage >= LOCATION_ACCURACY && !isEdge) {
            //            mUserPaint.setColor(Color.GREEN);
            //            Messager.Show("校准成功!", 0);
            mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_green_user), mBeaconRadius << 1, mBeaconRadius << 1);
            isEdge = true;
        } else if (percentage < LOCATION_ACCURACY && isEdge) {
            //            mUserPaint.setColor(Color.RED);
            mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_red_user), mBeaconRadius << 1, mBeaconRadius << 1);
            isEdge = false;
        }
        mUserCirRadius = (int) (mFullRadius * percentage);
    }

    private boolean isOnCenter = false;

    public void onCenter(boolean isOnCenter) {
        this.isOnCenter = isOnCenter;
        if(!isOnCenter){
            mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_red_user), mBeaconRadius << 1, mBeaconRadius << 1);
        }
        invalidate();
    }

}
